<?php
/**
 * polwebgis
 * 
 * This file is part of polwebgis.
 *
 * @copyright   Copyright (c) 2012 Dominik Janisch (http://www.joanneum.at)
 * @lastmodified    $LastChangedDate: 2012-03-07 09:15:12 +0000 (Wed, 07 Mar 2012) $
 * @revision    $Revision: 160 $ 
 */

require_once(PATH_TO_APP . "/components/gismap/GisMap.php");
require_once(PATH_TO_APP . "/components/gismap/BaseLayer.php");
require_once(PATH_TO_APP . "/components/tabledatagateway/GisMapGateway.php");
require_once(PATH_TO_APP . "/components/maplist/MapList.php");

class InputMapper
{  
   private $map_id_;
   private $singleton_registry_;
   private $table_gateway_;
   private $output_handler_ ;
   private $request_;
   private $session_;
   private $language_handler_;
   private $map_list_;
   private $session_name_;
    
    const WMS_REQUEST_CAPABILITIES = 'GetCapabilities';
    
    const WMS_REQUEST_LEGEND = 'GetLegendGraphic';

    public function __construct($table_gateway, $singleton_registry)
    {
        $this->singleton_registry_ = $singleton_registry;
        $this->table_gateway_ = $table_gateway;
        $this->output_handler_ = $singleton_registry->getSingleton('OutputHandler');
        $this->request_ = $singleton_registry->getSingleton('Request');
        $this->session_ = $singleton_registry->getSingleton('Session');
        $this->language_handler_ = $singleton_registry->getSingleton('LanguageHandler');
        $this->map_list_ =  new MapList();
    }
        
    public function __destruct(){}
    
    public function checkInput($is_wms)
    {
        $this->map_id_ = (int)$this->request_->getParameter('get', 'm_id');
        if($this->map_id_ < 1)
            throw new Exception('Illegal Map-ID');
        $this->session_name_ = "Map_" . $this->map_id_;
        $this->loadSession($this->session_name_);
        $this->map_list_->set('map_id', $this->map_id_);
        $this->map_list_->set('session_name', $this->session_name_);
        if($this->request_->issetParameter('get', 'REQUEST') && (strtolower($this->request_->getParameter('get', 'REQUEST')) == strtolower(constant("self::WMS_REQUEST_CAPABILITIES")) || strtolower($this->request_->getParameter('get', 'REQUEST')) == strtolower(constant("self::WMS_REQUEST_LEGEND"))))
        {
            $gm = new GisMap($is_wms, new GisMapGateway(), $this->singleton_registry_, $this->map_id_, &$this->map_list_, new BaseLayer(&$this->map_list_));
            $is_legend = strtolower($this->request_->getParameter('get', 'REQUEST')) == strtolower(constant("self::WMS_REQUEST_LEGEND")) ? true : false;
            $gm->wmsRequest($is_legend, $this->map_id_);
            $this->saveSession();
            return true;
        }
        $input = $this->request_->getParameter('get', 'input');
        $map_errorless = true;
        if($this->request_->issetParameter('get', 'selection') && $this->request_->getParameter('get', 'selection') == "region")
        {
            $this->editRegions(new GisMapGateway());
            $this->saveSession();
            return $map_errorless;
        }
        switch($input)
        {
            case 'wms':
                $gm = new GisMap($is_wms, new GisMapGateway(), $this->singleton_registry_, $this->map_id_, &$this->map_list_, new BaseLayer(&$this->map_list_));
                $map_errorless = $gm->init();
                break;
            case 'map':
                if($this->request_->issetParameter('get', 'selection') && $this->request_->getParameter('get', 'selection') == "region")
                {
                    $this->editRegions(new GisMapGateway());
                }
                else
                {
                    $gm = new GisMap($is_wms, new GisMapGateway(), $this->singleton_registry_, $this->map_id_, &$this->map_list_, new BaseLayer(&$this->map_list_));
                    $map_errorless = $gm->init();
                }
                break;
        }
        $this->saveSession();
        return $map_errorless;
    }
    
    private function loadSession($session_name)
    {
        $arr_variables = $this->session_->get($session_name.'.arr_params');
        if($arr_variables === false)
            return false;
        $this->map_list_->setArray($arr_variables);
    }
    
    private function saveSession()
    {
        $arr_params = $this->map_list_->getArray();
        $this->session_->set($this->session_name_.'.arr_params', $arr_params);
    }
    
    private function editRegions($gmtg)
    {
        $table_details = $gmtg->getDataDetails($this->map_id_);
        $arr_regions = $this->table_gateway_->getRegions($table_details->e_id);
        $arr_layer_gids = $this->map_list_->get('arr_layer_gids');
        $regionscount = $arr_layer_gids === false ? 0 : count($arr_layer_gids); 
        if(count($this->request_->getArray('post')) > 0)
        {
            $arr_layer_gids = array();
            foreach($this->request_->getParameter('post', 'arr_regions') as $item)
            {
                $arr_layer_gids[$item] = (int)$item;
            }
            $this->map_list_->set('arr_layer_gids', $arr_layer_gids);
        }
        if($this->request_->issetParameter('post', 'ajax'))
        {
            $arr_return['message'] = $this->language_handler_->getLanguageString('success_message_layer_props');
            $this->output_handler_->showJSON($arr_return);
        }
        else
        {
            $this->output_handler_->assignValuesToTemplate(array("arr_regions" => $arr_regions, "m_id" => $this->map_id_, "arr_layer_gids" => $arr_layer_gids, "regionscount" => $regionscount, "map_title" => $table_details->dt_titel));
            $this->output_handler_->showTemplate("selection");
        }
    }
}

?>
